package com.wfx.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jwt.util.JwtUtils;
import com.jwt.util.RsaUtils;
import com.wfx.entity.Result;
import com.wfx.entity.UserInfo;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.util.ResourceUtils;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.PublicKey;
import java.util.List;

/**
 * <p>title: com.wfx.filter</p>
 * author zhuximing
 * description:
 */

public class MyBasicAuthenticationFilter extends BasicAuthenticationFilter {


    public MyBasicAuthenticationFilter(AuthenticationManager authenticationManager) {
        super(authenticationManager);
    }



    //重写过滤器核心方法

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
       //1:获取jwt令牌
        String token = request.getHeader("token");
        if (token == null) {
            response(response,new Result(false,"请指定token"));
            return;

        }
      try {
          //2:解析jwt令牌
          String publicKeyPath = ResourceUtils.getFile("classpath:ras_public").getPath();
          PublicKey publicKey = RsaUtils.getPublicKey(publicKeyPath);
          //解析数据
          UserInfo userInfo = (UserInfo) JwtUtils.getInfoFromToken(token, publicKey, UserInfo.class);
          //获取权限
          String joinStr = userInfo.getRoleAndAuth();
          List<GrantedAuthority> grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(joinStr);
          //放入springsecurity的上下文
          UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userInfo, null, grantedAuthorities);
          SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
          //放行
          chain.doFilter(request, response);

      }catch (Exception e){
          e.printStackTrace();
          //  令牌解析失败
          response(response,new Result(false,"请输入正确的令牌"));
      }



    }


    private void  response(HttpServletResponse response, Result res){
       try {
           response.setContentType("application/json;charset=utf-8");
           response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
           PrintWriter out = response.getWriter();
           out.write(new ObjectMapper().writeValueAsString(res));
           out.flush();
           out.close();
       }catch (Exception e){

       }

    }
}